/**
 * Copyright (c) 2006 eMundo GmbH. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 *
 * Project: agil-client-web
 * File: StringUtil.java
 *
 */
package eu.emundo.agilpro.web.client.util;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Utility-class to work with Strings
 * 
 * @author Fabian Wohlschl\u00E4ger
 */
public class StringUtil {
	/**
	 * Replaces parts of a string
	 * 
	 * @param pSource
	 *            Source-string
	 * @param pSearch
	 *            Pattern-string
	 * @param pReplace
	 *            String which should replace the pattern-string
	 * @return Processed string
	 */
	public static String replaceAll(String pSource, String pSearch, String pReplace) {
		if((pSource == null) || (pSearch == null) || (pReplace == null))	{
			throw new IllegalArgumentException("Arguments should not be null");
		}
		
		// If nothing should be done
		if (pSearch.equals(pReplace)) {
			return pSource;
		}

		// Prevents continuous loop when search.equals("");
		StringBuffer lResult = new StringBuffer();
		int lLen = pSearch.length();
		if (lLen == 0) {
			return pSource;
		}

		int lPos = 0; // position
		int lNPos; // next position
		do {
			lNPos = pSource.indexOf(pSearch, lPos);
			// found
			if (lNPos != -1) {
				lResult.append(pSource.substring(lPos, lNPos));
				lResult.append(pReplace);
				lPos = lNPos + lLen;
			}
			// not found
			else {
				// last part
				lResult.append(pSource.substring(lPos));
			}
		} while (lNPos != -1);

		return lResult.toString();
	}

	/**
	 * Extracts the values of a sign-separated String (like a CSV-dataset)
	 * 
	 * @param pSource
	 *            Source-string
	 * @param pSeparator
	 *            Sepatator-string
	 * @return values
	 */
	public static String[] split(String pSource, String pSeparator) {
		if((pSource == null) || (pSeparator == null))	{
			throw new IllegalArgumentException("Arguments should not be null");
		}
		
		// If nothing should be done
		if(pSource.equals("") || pSeparator.equals(""))	{
			String[] lResult = { pSource };
			return lResult;
		}
		
		// Stores values
		ArrayList lList = new ArrayList();

		int pSeparatorLength = pSeparator.length();

		// Initialization with "", if the first value is ""
		StringBuffer lValue = new StringBuffer("");

		// If pSource contains one character
		if (pSource.length() <= pSeparatorLength) {
			// If the line contains two empty values
			if (pSource.equals(pSeparator)) {
				lValue = new StringBuffer("");
				lList.add(lValue.toString());
				lList.add(lValue.toString());
			}
			// If line contains one value (also an empty)
			else {
				lList.add(pSource);
			}
		} else {
			// Works character by character
			String lCharacter = "";

			for (int lI = 0, lIMax = pSource.length(); lI < lIMax; lI++) {
				lCharacter = Character.toString(pSource.charAt(lI));

				// If character isn't a part of a value
				if ((pSeparator.indexOf(lCharacter) != -1)
						&& (pSeparator.equals(pSource.substring(lI, lI + pSeparatorLength)))) {
					// If there is an empty field at the end of the line
					if (lI == (pSource.length() - pSeparatorLength)) {
						lValue = new StringBuffer("");
						// If there was an empty field before
						if (pSource.substring(lI - pSeparatorLength, lI).equals(pSeparator))
							lList.add(lValue.toString());
					}

					lList.add(lValue.toString());
					// Deletes content of value
					lValue = new StringBuffer("");

					lI += pSeparatorLength - 1;
				}
				// If character is a part of a value
				else {
					lValue.append(lCharacter);

					// Store last value
					if (lI == (pSource.length() - 1))
						lList.add(lValue.toString());
					// Store before last value if separator follows
					else if ((lI == (pSource.length() - pSeparatorLength))
							&& pSource.substring(lI - pSeparatorLength, lI).equals(pSeparator))
						lList.add(lValue.toString());
				}
			}
			/**/
		}

		/* Transform ArrayList to String[] */
		String[] lResult = new String[lList.size()];
		Iterator lIt = lList.iterator();
		int lResultIndex = 0;

		while (lIt.hasNext()) {
			lResult[lResultIndex] = (String) lIt.next();
			lResultIndex++;
		}
		/**/

		return lResult;
	}
	
	/**
	 * Tests equality of two string arrays
	 * 
	 * @param pArray1 String-array
	 * @param pArray2 String-array
	 * @return true if they are equal
	 */
	public static boolean equals(String[] pArray1, String[] pArray2)	{
		if((pArray1 == null) || (pArray2==null))	{
			throw new IllegalArgumentException("null isn't a legal argument");
		}
		
		if(pArray1.length != pArray2.length)	{
			return false;
		}
		
		for(int lI=0, lIMax=pArray1.length; lI < lIMax; lI++)	{
			if(!pArray1[lI].equals(pArray2[lI]))	{
				return false;
			}
		}
		
		return true;
	}
}
